<?php
App::uses('PaymentAppModel', PAYMENT_CAMELCASENAME.'.Model');
/**
 * PluginPaymentPagamento Model
 *
 * @property PagamentoSystem $PagamentoSystem
 * @property PaymentGateway $PaymentGateway
 * @property PrimaryPaymentMethod $PrimaryPaymentMethod
 * @property SecondaryPaymentMethod $SecondaryPaymentMethod
 * @property Status $Status
 */
class PaymentPagamento extends PaymentAppModel {

/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'payment_gateway_id';
	public $useTable = 'plugin_payment_pagamentos';

	public $belongsTo = array(
		'PaymentGateway' => array(
			'className' => 'PaymentPlugin.PaymentGateway',
			'foreignKey' => 'payment_gateway_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'PrimaryPaymentMethod' => array(
			'className' => 'PaymentPlugin.PaymentPaymentMethod',
			'foreignKey' => 'primary_payment_method_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'SecondaryPaymentMethod' => array(
			'className' => 'PaymentPlugin.PaymentPaymentMethod',
			'foreignKey' => 'secondary_payment_method_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Status' => array(
			'className' => 'PaymentPlugin.PaymentStatus',
			'foreignKey' => 'status_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	public function __construct($id = false, $table = null, $ds = null){
		$this->belongsTo['PaymentGateway']['className'] = PAYMENT_CAMELCASENAME.'.PaymentGateway';
		$this->belongsTo['PrimaryPaymentMethod']['className'] = PAYMENT_CAMELCASENAME.'.PaymentPaymentMethod';
		$this->belongsTo['SecondaryPaymentMethod']['className'] = PAYMENT_CAMELCASENAME.'.PaymentPaymentMethod';
		$this->belongsTo['Status']['className'] = PAYMENT_CAMELCASENAME.'.PaymentStatus';
		parent::__construct();
	}
	
	public function createTransaction($data, $config){
		$save['model_pagamento'] = $config['model'];
		$save['pagamento_system_id'] = $config['id'];
		$save['payment_gateway_id'] = 0;
		$save['gateway_code'] = '0';
		$save['primary_payment_method_id'] = 0;
		$save['secondary_payment_method_id'] = 0;
		$save['status_id'] = 0;
		return $this->save($save);
	}
	
	public function updateGateway($gateway, $id){
		$this->id = $id;
		return $this->saveField('payment_gateway_id', $gateway);
	}
	
	public function saveRedirectCodeAndDate($id, $dados){
		return $this->save(array('id'=>$id, 'redirect_code'=>$dados['code'], 'date_gateway'=>$dados['date']));
	}
	
	public function saveTransactionCode($id, $transaction_code){
		return $this->save(array('id'=>$id, 'transaction_code'=>$transaction_code));
	}
	
	public function getIdByCode($code){
		$data = $this->findByTransactionCode($code);
		if($data)
			return $data[$this->alias]['id'];
		else return false;
	}
	
	public function atualizarTransaction($dados){
		$dataSource = $this->getDataSource();
		$dataSource->begin();
		$this->recursive = -1;
		$old_data = $this->findById($dados['id']);
		$this->bindModel(array('hasOne'=>array('PaymentLog'=>array('className'=>PAYMENT_CAMELCASENAME.'.PaymentLog'))));
		unset($old_data['PaymentPagamento']['id']);
		if($this->PaymentLog->save($old_data['PaymentPagamento']) && $this->save($dados)){
			$dataSource->commit();
			return true;
		}
		else {
			$dataSource->rollback();
			return false;
		}
		
	}

	public function findIdsOfAllConfirmed($model){
		$ids_confirmados = $this->Status->idsPagos();
		return $this->find('list', array('conditions'=>array('status_id'=>$ids_confirmados), 'fields'=>array('pagamento_system_id')));
	}
}
